/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package handler;

import database.DatabaseHandler;
import java.sql.ResultSet;
import java.sql.SQLException;
import model.Bruger;
import utility.Adgangskodekryptering;

/**
 *
 * @author Jeppe
 */
public class BrugerHandler {

    private final DatabaseHandler databaseHandler;

    protected BrugerHandler() {
        this.databaseHandler = DatabaseHandler.getInstance();
    }

    public Bruger getBrugerByTlf(String tlf) throws SQLException {
        Bruger enBruger = null;
        String sql = "Call getBrugerByTlf('" + tlf + "')";

        ResultSet rs = databaseHandler.select(sql);
        if (rs.next()) {
            enBruger = new Bruger(rs.getString("tlf"), rs.getString("email"), rs.getString("fornavn"), rs.getString("mellemnavn"), rs.getString("efternavn"), rs.getString("passphrase"));
        }
        return enBruger;
    }

    public boolean isPasswordCorrectTlf(String tlf, String password) throws SQLException {
        boolean correct = false;
        ResultSet rs = databaseHandler.select("Call confirmPassByTlf('" + tlf + "')");
        if(rs.next()){
        correct = Adgangskodekryptering.isTheSameMD5(rs.getString("passphrase"), password);
        }
        return correct;
    }

    public Bruger getBrugerByEmail(String email) throws SQLException {
        Bruger enBruger = null;
        String sql = "Call getBrugerByEmail('" + email + "')";

        ResultSet rs = databaseHandler.select(sql);
        if (rs.next()) {
            enBruger = new Bruger(rs.getString("tlf"), rs.getString("email"), rs.getString("fornavn"), rs.getString("mellemnavn"), rs.getString("efternavn"), rs.getString("passphrase"));
        }
        return enBruger;
    }

    public boolean isPasswordCorrectEmail(String email, String password) throws SQLException {
        boolean correct = false;
        ResultSet rs = databaseHandler.select("Call confirmPassByEmail('" + email + "')");
        if(rs.next()){
        correct = Adgangskodekryptering.isTheSameMD5(rs.getString("passphrase"), password); 
        }
        return correct;
    }
}
